function parseLyric(lrc) {
  var lyrics = lrc.split("\n");
  var lrcObj = {};
  for (var i = 0; i < lyrics.length; i++) {
    var lyric = decodeURIComponent(lyrics[i]);
    var timeReg = /\[\d*:\d*((\.|\:)\d*)*\]/g;
    var timeRegExpArr = lyric.match(timeReg);
    if (!timeRegExpArr) continue;
    var clause = lyric.replace(timeReg, '');
    for (var k = 0, h = timeRegExpArr.length; k < h; k++) {
      var t = timeRegExpArr[k];
      var min = Number(String(t.match(/\[\d*/i)).slice(1)),
        sec = Number(String(t.match(/\:\d*/i)).slice(1));
      var time = min * 60 + sec;
      lrcObj[time] = clause;
    }
  }
  return lrcObj;
}
Page({
  data: {
    songList: [],
    songIdList: [],
    scrollHeight: 0,
    size: 5,
    offset: 0,
    type: 1,
    page: 1,
    song_id: 0,
    music: {},
    playingMusicId: 0,
    musicPercent: 0,
    musicLrc: '',
    musicLineLrc: ''
  },
  onLoad: function () {
    var that = this;
    this.audioCtx = wx.createAudioContext('myAudio')
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          scrollHeight: res.windowHeight - 100
        })
      }
    })
    wx.showLoading({
      title: '数据加载中...'
    })
    wx.request({
      url: 'http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.billboard.billList',
      header: {
        'content-type': 'application/json'
      },
      data: {
        type: that.data.type,
        size: that.data.size,
        offset: that.data.offset
      },
      success: function (res) {
        var idList = [];
        for (var i = 0; i < res.data.song_list.length; i++) {
          idList.push(res.data.song_list[i].song_id)
        }
        that.setData({
          songIdList: idList,
          songList: res.data.song_list
        })
        wx.hideLoading()
        that.data.page++;
        that.playIdMusic(that.data.songIdList[that.data.playingMusicId]);
      }
    })

  },
  loadMore: function () {
    var that = this;
    wx.showLoading({
      title: '数据加载中...'
    })
    var newOffset = (that.data.page - 1) * that.data.size;
    wx.request({
      url: 'http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.billboard.billList',
      header: {
        'content-type': 'application/json'
      },
      data: {
        type: that.data.type,
        size: that.data.size,
        offset: newOffset
      },
      success: function (res) {
        var idList = [];
        for (var i = 0; i < res.data.song_list.length; i++) {
          that.data.songList.push(res.data.song_list[i])
          that.data.songIdList.push(res.data.song_list[i].song_id);
        }
        that.setData({
          songIdList: that.data.songIdList,
          songList: that.data.songList
        })
        wx.hideLoading()
        that.data.page++;

      }
    })

  },
  playNextMusic: function (event) {
    var that = this;
    var nextMusicPos = this.data.playingMusicId + 1;
    var nextMusicSongId = this.data.songIdList[nextMusicPos];
    this.playIdMusic(nextMusicSongId);
    this.setData({
      playingMusicId: nextMusicPos
    })
  },
  pauseMusic: function (event) {
    console.log(this.data.playingMusicId)
  },
  playIdMusic: function (songId) {
    var that = this;
    var url = 'http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.song.play&songid=' + songId;
    wx.request({
      url: url,
      success: function (res) {
        that.setData({
          music: res.data
        })
        wx.request({
          url: res.data.songinfo.lrclink,
          success: function (res) {
            that.setData({
              musicLrc: parseLyric(res.data)
            })
            setTimeout(function () {
              that.audioCtx.play()
            }, 500)

          }
        })
      }
    })
  },
  playMusic: function (event) {
    var that = this;
    var url = 'http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.song.play&songid=' + event.currentTarget.dataset.songid;
    wx.request({
      url: url,
      success: function (res) {
        that.setData({
          music: res.data
        })
        wx.request({
          url: res.data.songinfo.lrclink,
          success: function (res) {
            that.setData({
              musicLrc: parseLyric(res.data)
            })
            that.audioCtx.play();
          }
        })
      }
    })
  },
  changeMusicLrc: function (event) {
    var timePosition = Math.floor(event.detail.currentTime);
    var musicPercent = parseInt(event.detail.currentTime / event.detail.duration * 100);
    var that = this;
    that.setData({
      musicPercent: musicPercent,
      musicLineLrc: that.data.musicLrc[timePosition]
    })
  }
})